#!/usr/bin/python

""" Kill cassandra server
"""

import os, sys, time, datetime

def get_user_name_from_uid(uid):
    if not uid.isdigit():
        return uid
    else:
        s = os.popen('getent passwd ' + uid).read()
        return s.split(':')[0]
    
if __name__ == '__main__':
    process_str = 'CassandraDaemon'
    
    lines = os.popen('ps -A -opid,user,etime,args --sort=pcpu | grep ' + process_str).read().strip().split('\n')
    
    proc_list = []
    for line in lines:
        if 'grep' not in line:
            # print line
            items = line.split()
            if items[0].isdigit():
                pid = items[0]
                user_name = items[1]
                if user_name.isdigit():
                    user_name = get_user_name_from_uid(user_name)
                etime = items[2].replace('-', 'd ')
                tmp_list = items[3:]
                command = ''
                for item in tmp_list:
                    command += item + ' '
            proc_list.append((pid, user_name, etime, command))
            
    print '\033[1;96mPid\tCassandra Server\033[0m'
    # display the cassandra server
    for items in proc_list:
        pid = items[0]
        user_name = items[1]
        etime = items[2]
        command = items[3]
        
        day = 0
        i = etime.find('d')
        if i > 0:
            day = int(etime[:i])
            
            
        if len(etime) < 6:
            hour = 0
            minute = int(etime[: 2])
            second = int(etime[3: ])
        else:
            j = etime.find(':')
            k = etime.rfind(':')
            hour = int(etime[j - 2 : j])
            minute = int(etime[j + 1 : k])
            second = int(etime[k + 1 : ])
                        
        curr_time = datetime.datetime.now()
        s_time = curr_time - datetime.timedelta(days = day,hours = hour, minutes = minute, seconds = second)
        start_time = str(s_time)[:18] + str(round(float(str(s_time)[18:])))[0]
        
        
        
        PIG_INITIAL_ADDRESS = ''
        PIG_RPC_PORT = ''
        lines = os.popen('cat /proc/' + pid + '/environ').read().split('\0')
        for line in lines:
            if line.startswith('PWD'):
                i = line.find('=')
                #print line
                pwd_str = line[i + 1: ]
            elif line.startswith('PIG_INITIAL_ADDRESS'):
                i = line.find('=')
                #print line
                PIG_INITIAL_ADDRESS = line[i + 1: ]
            elif line.startswith('PIG_RPC_PORT'):
                i = line.find('=')
                #print line
                PIG_RPC_PORT = line[i + 1: ]
                
                
        i = command.find(' -cp ')
        dir_str = ''
        if i > 0:
            s = command[i + 5 : ]
            j = s.find('/../')
            dir_str = s[ : j]
            
        
        if not dir_str.startswith('/'):
            dir_str = pwd_str + '/' + dir_str
        print '\033[1;41m' + pid + '\033[0m\t' + '\033[94mBase Dir :\033[0m ' + dir_str
        #print '\t\033[94mPig RPC  :\033[0m ' + PIG_INITIAL_ADDRESS + ':' + PIG_RPC_PORT
        print '\t\033[94mStarted  :\033[0m ' +  start_time
        print '\t\033[94mElapsed  :\033[0m ' + etime
        print
        
        
        
        
        
        
    


